package ita.class1.group2.supermarket.administrator.controller;

import ita.class1.group2.supermarket.administrator.pojo.AdForm;
import ita.class1.group2.supermarket.administrator.pojo.Identity;
import ita.class1.group2.supermarket.administrator.pojo.Store;
import ita.class1.group2.supermarket.administrator.pojo.StoreForm;
import ita.class1.group2.supermarket.administrator.service.*;
import ita.class1.group2.supermarket.common.util.RespMessageFactory;
import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.annotation.RequestMapping;
import ita.class1.group2.supermarket.common.pojo.ResponseMessage;

import java.util.Map;

@Component
public class MApiController {

    @Autowired
    IdentityService identityService;
    @Autowired
    StoreService storeService;
    @Autowired
    StoreFormRecordService storeFormRecordService;
    @Autowired
    StoreFormService storeFormService;
    @Autowired
    AdFormService adFormService;
    /**
     * 获取状态
     *
     * @throws Exception
     */
    @RequestMapping("/m/api/check")
    public ResponseMessage check(Map<String, String> map) throws Exception {
        String user_id = map.get("USER_ID");
        return RespMessageFactory.createSuccessMessage(storeFormRecordService.getStoreFormRecordByUid(user_id));
    }

    /**
     * 获取商铺信息
     *
     * @throws Exception
     */
    @RequestMapping("/m/api/getStore")
    public ResponseMessage getStore(Map<String, String> map) throws Exception {
        String user_id = map.get("USER_ID");
        Store store = storeService.getStoreByUid(user_id);
        return RespMessageFactory.createSuccessMessage(store);
    }

    /**
     * 获取商家信息
     *
     * @throws Exception
     */
    @RequestMapping("/m/api/getIndentity")
    public ResponseMessage getIndentity(Map<String, String> map) throws Exception {
        String identity_id = map.get("IDENTITY_ID");
        Identity identity = identityService.getIdentity(identity_id);
        return RespMessageFactory.createSuccessMessage(identity);
    }

    /**
     * @throws Exception
     */
    @RequestMapping("/m/api/storeForm")
    public ResponseMessage getStoreForm(Map<String, String> map) throws Exception {
        String user_id = map.get("USER_ID");
        StoreForm storeForm = storeFormService.getStoreForm(user_id);
        return RespMessageFactory.createSuccessMessage(storeForm);
    }

    @RequestMapping("/m/api/AdStatus")
    public ResponseMessage getAdStatusByUser(Map<String, String> map) throws Exception {
        String user_id = map.get("USER_ID");
        StoreForm storeForm = storeFormService.getStoreForm(user_id);
        AdForm adForm = adFormService.getAdStatusBySid(storeForm.getStore().getId());
        if(adForm != null && adForm.getConsumeTime() != null){
            return RespMessageFactory.createSuccessMessage(true);
        }else{
            return RespMessageFactory.createSuccessMessage(false);
        }
    }


}
